<?php

class TypeController extends Silk_Controller_Admin_Action {

    public function init(){
	parent::init();
	$this->appendBreadcrumbs(Silk::Translate('Manage Types'), $this->resolveRoute('type_admin'));
    }

    public function adminAction(){

	$this->initPaginatorParams('silk_cms_types___', 'name');

	$session = Silk::getSession();

	if ($this->_request->has('criteria'))
	    $session->criteria = $this->_getParam('criteria');
	else
	    $session->criteria = null;

	$Types = new Silk_Types();

	$paginator = $Types->getPaginator($session->criteria, "{$session->sort} {$session->order}");

	$paginator->setItemCountPerPage($session->limit);
	$paginator->setCurrentPageNumber($session->page);

	$this->view->types = $paginator;

    }

    public function createAction(){
	$Types = new Silk_Types();

	if ($this->_request->isPost()){

	    try {

		$type = $Types->create($this->_getParam('type'));
		$type->virtual = true;
		$type->save();

		$role_permissions = ($this->_getParam('role_permissions')) ? $this->_getParam('role_permissions') : array();
		$user_permissions = ($this->_getParam('user_permissions')) ? $this->_getParam('user_permissions') : array();

		$Types->setPermissions($type, $role_permissions, $user_permissions);

		$this->gotoRoute('type_admin');

	    } catch (FP_Validation_Exception $x){

		foreach ($x->getValidationErrors() as $error) $this->notifications($error, 'error');
	    }

	} else
	    $type = $Types->create();

	$default_privileges = array(PERMISSION_CREATE, PERMISSION_VIEW, PERMISSION_EDIT, PERMISSION_DELETE);

	$Roles = new Silk_Roles();

	$this->view->type = $type;
	$this->view->resource = null;
	$this->view->roles = $Roles->fetch();
	$this->view->users = array();
	$this->view->privileges = $default_privileges;
    }

    public function editAction(){

	$Types = new Silk_Types();
	$type  = $Types->fetch($this->_getParam('type_id'));

	if ($type){
	    if ($this->_request->isPost()){

		try {

		    $type->import($this->_getParam('type'));
		    $type->save();

		    $role_permissions = ($this->_getParam('role_permissions')) ? $this->_getParam('role_permissions') : array();
		    $user_permissions = ($this->_getParam('user_permissions')) ? $this->_getParam('user_permissions') : array();

		    $Types->setPermissions($type, $role_permissions, $user_permissions);

		    $this->gotoRoute('type_admin');

		} catch (FP_Validation_Exception $x){

		    foreach ($x->getValidationErrors() as $error) $this->notifications($error, 'error');
		}

	    }

	    $permissions = $type->getPermissions();

	    $privileges = array();

	    foreach ($permissions as $permission){
		if (!in_array($permission->privilege, $privileges))
		    $privileges[] = $permission->privilege;
	    }

	    if (!in_array(PERMISSION_CREATE, $privileges))
		$privileges[] = PERMISSION_CREATE;

	    if (!in_array(PERMISSION_EDIT, $privileges))
		$privileges[] = PERMISSION_EDIT;

	    if (!in_array(PERMISSION_VIEW, $privileges))
		$privileges[] = PERMISSION_VIEW;

	    if (!in_array(PERMISSION_DELETE, $privileges))
		$privileges[] = PERMISSION_DELETE;


	    $Roles = new Silk_Roles();

	    $this->view->type = $type;
	    $this->view->roles = $Roles->fetch();
	    $this->view->users = array();
	    $this->view->privileges = $privileges;
	    $this->view->resource = "type.{$type->type_id}";
	    $this->view->permissions = $permissions;
	} else
	    throw new FP_Route_Exception('Invalid type id', FP_Route_Exception::NO_RESOURCE);
    }

    public function deleteAction(){
	$Types = new Silk_Types();
	$type  = $Types->fetch($this->_getParam('type_id'));

	if ($type){
	    if ($this->_request->isPost()){

		try {

		    $type->delete();
		    $this->gotoRoute('type_admin');

		} catch (FP_Validation_Exception $x){

		    foreach ($x->getValidationErrors() as $error) $this->notifications($error, 'error');
		}

	    }

	    $this->view->type = $type;
	} else
	    throw new FP_Route_Exception('Invalid type id', FP_Route_Exception::NO_RESOURCE);
    }
}

?>